# -*- coding: utf-8 -*-

import os, socket, logging, redis, pickle, json
from redis import Redis

redis_pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=0, password='Schenker123')
redis_instance = redis.Redis(connection_pool=redis_pool)

# f = open('/root/jdbook/jdbook.json', encoding='utf-8')

# book_list = json.load(f)

# for book in book_list:
#     print(book['name'])

with open('jd_book.json') as f:
    for line in f:
        book = json.loads(line)
        sku = book['sku']
        try:
            int(sku)
            if not redis_instance.hexists(name='jdbook', key=sku):
                redis_instance.hset(name='jdbook', key=key, value=pickle.dumps(book))
        except:
            print('Error for:%s' %sku)
    print('job done')


'''
    book = pickle.loads(r.hget(name='jdbook', key=item.sku.strip))
    item.url = book.get('url','').strip()
    item.sku = book.get('sku','').strip()
    item.name = book.get('name','').strip()
    # item.name_py
    # item.price
    # item.top_category
    # item.sub_category
    # item.summary
    # item.mall
    item.pic_url = book.get('img','').strip()
    item.author = book.get('author','').strip()
    item.pub = book.get('pub','').strip()
    item.pub_date = book.get('pub_date','').strip()
    # item.url_refer
    # item.url_refer_mb
    # item.grade
    # item.qiniu_pic
    item.active = True
    item.save()
'''